package com.acme.bankapp.service.bank;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;

public class BankService {
	private File clientFilePath = new File("src/Files/clients");
	private File bankFile = new File("src/Files/bank");

	public void addClient(Bank bank, Client cl) {
		bank.addclient(cl);
		BankDataLoader.appendClientToFile(clientFilePath, cl);
	}

	public void printMaximumAmountToWithdraw(Bank bank) {
		Client masc[] = bank.getClients();
		for (Client cl : masc) {
			if (cl != null)
				System.out.println(cl.getClientSalutation() + " "
						+ cl.getAcc(0).maximumAmountToWithdraw());
		}
	}

	public void saveBank(Bank b, File pathTo) {
		FileOutputStream out;
		ObjectOutputStream objout;
		try {
			out = new FileOutputStream(pathTo);
			objout = new ObjectOutputStream(out);
			objout.writeObject(b);
			objout.close();
			out.close();
		} catch (IOException e) {
		}
	}

	public Bank loadBank(File pathFrom) {
		if (!pathFrom.exists()) {
			System.out.println("load path not found");
			return null;
		}

		Bank bank = null;
		try {
			FileInputStream in = new FileInputStream(pathFrom);
			ObjectInputStream objin = new ObjectInputStream(in);
			bank = (Bank) objin.readObject();
			objin.close();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
		}
		return bank;
	}

	public File getClientSavePath() {
		return clientFilePath;
	}

	public void setClientSavePath(File clientSavePath) {
		this.clientFilePath = clientSavePath;
	}

	public File getBankFilePath() {
		return bankFile;
	}

	public void setBankFilePath(File bankFile) {
		this.bankFile = bankFile;
	}

}
